﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour {
	public static AudioManager instance;
	public AudioSource source;
	public GameObject[] disableSound;
	public static bool CanPlay
	{
		get{ return !instance.source.mute;}
	}
	public static int AudioStage
	{
		get{ return PlayerPrefs.GetInt ("audio");}
		set
		{
			PlayerPrefs.SetInt ("audio", value);
		}
	}
	private void Awake()
	{
		instance = this;
		if (source == null)
			source = GetComponent<AudioSource> ();
	}
	public static void SetAudio()
	{
		if (AudioStage == 0)
			AudioStage = 1;
		else if (AudioStage == 1)
			AudioStage = 0;
		CheckAudioStage ();
	}
	public static void CheckAudioStage()
	{
		if (AudioStage == 0) {
			instance.source.mute = false;
			foreach (var item in instance.disableSound) {
				item.SetActive (false);
			}
		} else {
			instance.source.mute = true;
			foreach (var item in instance.disableSound) {
				item.SetActive (true);
			}
		}

		PlayBackground ();
	}
	public static void HideDisableIcon()
	{
		instance.disableSound [1].SetActive (false);
	}
	public AudioClip backgroundClipMenu;
	public AudioClip backgroundClipGame;
	public static void PlayBackground()
	{
		if (CanPlay) {
			if(GameManager.gameStage==GameStage.WAIT)
				instance.source.clip =instance.backgroundClipMenu;
			else if(GameManager.gameStage==GameStage.PLAY||GameManager.gameStage==GameStage.PAUSE)
				instance.source.clip =instance.backgroundClipGame;
			instance.source.Play ();
			instance.source.loop = true;
		} else {
			instance.source.clip = null;
			instance.source.Stop ();
		}
	}
	public AudioClip[] clip;
	public static void PlayOneShoot(int index)
	{
		instance.source.PlayOneShot (instance.clip [index]);
	}
}
